package jschool.factorization;

import java.util.ArrayList;
import java.util.List;


public class Factorizator implements Computable<Integer, List<Integer>> {
	
	private int counter = 0;
	private List<Integer> primes;
	
	public void initPrimes(int max) {
		primes = new ArrayList<>();
		outer:
		for (int i = 2; i <= max; i++) {
			for (int j = 2; j <= i / 2; j++) {
				if (i % j == 0) {
					continue outer;
				}
			}
			primes.add(i);
		}
	}
	
	public int getCount() {
		return counter;
	}
	
	public List<Integer> factorize(int num) {
		if (num <= 1) 
			throw new IllegalArgumentException("num should be greater than 1");
		
		counter++;
		
		List<Integer> result = new ArrayList<>();
		for (int i = 0; i < primes.size(); i++) {
			int divider = primes.get(i);
			while (num % divider == 0) {
				result.add(divider);
				num /= divider;
			}
		} 
		
		return result;
	}

	@Override
	public List<Integer> compute(Integer arg) throws InterruptedException {
		return factorize(arg);
	}
}
